<?php
namespace common\models;


class TradeClub extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'trade_club';
    }


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['money', 'month_income', 'sell_start_time', 'sell_end_time'], 'required'],
            [['money', 'day_income', 'month_income'], 'number'],
            [['sell_start_time', 'sell_end_time', 'create_time', 'update_time'], 'safe'],
            [['img', 'period'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'commodity_id' => '产品id',
            'money' => '金额',
            'img' => '图片',
            'day_income' => '日利率',
            'month_income' => '月利率',
            'sell_start_time' => '销售开始时间',
            'sell_end_time' => '销售结束时间',
            'period' => '签约周期（自由理财无）',
            'create_time' => '创建时间',
            'update_time' => '修改时间',
        ];
    }    

	/*
		* 数据列表
		* whereArr 条件
		* params 基本参数 包含 field order page limit
		* extends  扩展信息 一些相关的信息
		* 
	*/	
	public static function dataList($whereArr, $params, $extends = array())
	{
		$model  = self::find();
		$where  = isset($whereArr['where']) ? $whereArr['where'] : [];
		$whereAnd = isset($whereArr['and'])?$whereArr['and']:[];
		$models = self::queryFormart($model,$where,$params,$whereAnd);
		$model  = $models['model'];
		
		$data  = $model->asarray()->all();
		if(!$data) return array();

		//扩展信息
		if(!$extends) return $data;
		foreach ($data as $k=>$v) {
            foreach ($extends as $extend) {
                if ($extend=='api'){
                    $instance = StaticInstance::getOne(['order_type'=>4,'member_id'=>$params['memberId'],'status'=>1,'commodity_id'=>$v['commodity_id']]);
                    if ($instance){
                        $data[$k]['is_join']=1;
                    }else{
                        $data[$k]['is_join']=0;
                    }
                }
            }
        }

		return $data;	
	}

	public static function getOne($where, $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->one();
	}


	public static function getAll($where = [], $field = ['*'])
	{
		return self::find()->select($field)->where($where)->asarray()->all();
	}

}